<!-- TODO -->
<template>
	<div>
		<el-card class="!border-none" shadow="never">
			<el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
				<el-form-item label="名称">
					<el-input
							class="w-[150px]"
							v-model="queryParams.name"
							placeholder="请输入名称"
							clearable
							@keyup.enter="resetPage"
					/>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="resetPage">查询</el-button>
					<el-button @click="resetParams">重置</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card class="!border-none mt-4" shadow="never">
			<div style="float: right">
				<el-button class="mb-4" v-perms="['auth.admin/add']" type="primary" @click="handleAdd" >
					添加入口
				</el-button>
			</div>
			<!--			<el-button class="mb-4" v-perms="['auth.admin/add']" type="primary" @click="handleAdd">-->
			<!--				一键删除-->
			<!--			</el-button>-->
			<el-table size="large" :data="pager.lists"   @selection-change="handleSelectionChange">
<!--				<el-table-column type="selection" width="55" />-->
<!--				<el-table-column label="排序权重" prop="id" min-width="100">-->
<!--					<template #default="{ row }">-->
<!--						<el-input-->
<!--								class="w-[100px]"-->
<!--								v-model="queryParams.user_keyword"-->
<!--								@keyup.enter="resetPage"-->
<!--						/>-->
<!--					</template>-->
<!--				</el-table-column>-->
				<el-table-column label="入口名称" prop="name" min-width="120" >
					<template #default="{ row }">
						{{ row.name }}
					</template>
				</el-table-column>
				<el-table-column label="LOGO" prop="logo" min-width="120" >
					<template #default="{ row }">
						<el-avatar
							:src="row.logo || row.goods_image"
							style="height: 60px; min-width: 60px"
						/>
					</template>
				</el-table-column>
				<el-table-column label="首页显示" prop="is_show" min-width="120" >
					<template #default="{ row }">
						<el-switch
								size="large"
								v-model="row.is_show"
								:active-value="1"
								:inactive-value="0"
								width="80px"
								inline-prompt
								style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
								active-text="显示"
								inactive-text="隐藏"
								@click="toggleDark(row)"
						/>
					</template>
				</el-table-column>
<!--				<el-table-column label="创建时间" prop="created_at" min-width="180" />-->
				<el-table-column label="操作" fixed="right" min-width="160">
					<template #default="{ row }">
						<el-button type="primary" link @click="handleEdit(row)"> 编辑 </el-button>
						<el-button type="danger" link @click="handleDelete(row.id)">
							删除
						</el-button>
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页 -->
			<div class="flex justify-end mt-4">
				<pagination v-model="pager" @change="getLists" />
			</div>
		</el-card>
		<edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
	</div>
</template>

<script lang="ts" setup>
	import { usePaging } from '@/hooks/usePaging'
	import { entranceList,stateEntrance,entranceAdd, detailEntrance, entranceEdit, decEntrance } from '@/api/reception'
	import EditPopup from './edit.vue'
	import feedback from '@/utils/feedback'
	import {ref} from "vue";
	import {useToggle} from "@vueuse/core";


	const queryParams = reactive({
		name: '',
	});

	const { pager, getLists, resetPage, resetParams } = usePaging({
		fetchFun: entranceList,
		params: queryParams
	})

	getLists()


	//删除
	const handleDelete = async (id : any) => {
		await feedback.confirm('确定要删除该入口？')
		await decEntrance({ entrance_id: id})
		getLists()
	}

	const editRef = shallowRef<InstanceType<typeof EditPopup>>()
	const showEdit = ref(false)

	const handleAdd = async () => {
		showEdit.value = true
		await nextTick()
		editRef.value?.open('add')
	}

	const handleEdit = async (row:any) => {
		showEdit.value = true
		await nextTick()
		editRef.value?.open('edit')
		editRef.value?.getDetail(row)
	}

	const toggleDark = (row: any) => {
		//1 显示 0 不显示
		stateEntrance({entrance_id: row.id, is_show: row.is_show})
	}

	const ids = ref<any[]>([])
	const handleSelectionChange = (val: any[]) => {
		ids.value = val.map((item) => {
			return item.id
		})
	}

</script>
<style lang="scss" scope>
	//TODO
	.el-alert--warning.is-light {
		background-color: rgba(246, 248, 255, 1);
		color: rgba(64, 115, 250, 1);
	}
</style>
